package org.example.service.imp;



import org.example.entity.VideoParseRecords;
import org.example.mapper.VideoParseRecordMapper;
import org.example.service.VideoParseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VideoParseRecordServiceImpl implements VideoParseRecordService {

    @Autowired
    private VideoParseRecordMapper videoParseRecordMapper;
    @Override
    public List<String> getAllPlatforms() {
        return videoParseRecordMapper.getAllPlatforms();
    }

    @Override
    public List<String> getAllNicknames() {
        return videoParseRecordMapper.getAllNicknames();
    }

    @Override
    public Map<String, Object> getVideoParseRecords(String nickname, String platform, String keyword, int page, int pageSize) {
        Map<String, Object> result = new HashMap<>();

        int offset = (page - 1) * pageSize;

        List<VideoParseRecords> records = videoParseRecordMapper.getVideoParseRecords(
                nickname, platform, keyword, offset, pageSize
        );

        // 确保字段名正确
        List<Map<String, Object>> formattedRecords = records.stream().map(record -> {
            Map<String, Object> formatted = new HashMap<>();
            formatted.put("nickname", record.getNickname());
            formatted.put("original_url", record.getOriginalUrl());
            formatted.put("platform", record.getPlatform());
            formatted.put("parse_time", record.getParseTime());
            return formatted;
        }).collect(Collectors.toList());

        int total = videoParseRecordMapper.countVideoParseRecords(nickname, platform, keyword);

        result.put("records", formattedRecords);
        result.put("total", total);
        result.put("currentPage", page);
        result.put("pageSize", pageSize);
        result.put("totalPages", (int) Math.ceil((double) total / pageSize));
        System.out.println("ceshi::::"+platform);
        return result;
    }


}